package xworker.gluon.attach;

import com.gluonhq.attach.settings.SettingsService;
import org.xmeta.ActionContext;
import org.xmeta.Thing;

public class SettingsActions {
    //private static final String TAG = SettingsActions.class.getName();

    public static void remove(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        String key = self.doAction("getKey", actionContext);

        SettingsService.create().ifPresent(service -> service.remove(key));
    }

    public static String retrieve(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        String key = self.doAction("getKey", actionContext);

        SettingsService service = SettingsService.create().orElse(null);
        if(service != null){
            return service.retrieve(key);
        }
        return null;
    }

    public static void store(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        String key = self.doAction("getKey", actionContext);
        String value = self.doAction("getValue", actionContext);

        SettingsService service = SettingsService.create().orElse(null);
        if(service != null){
            if(value == null){
                service.retrieve(key);
            }else{
                service.store(key, value);
            }
        }
    }
}
